const app = getApp()
const ajax = require('../../utils/ajax.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsAttribte:{},
    // bechoice: 0,
    // choiceColor: '',
    imgUrls: [],
    text_list:[
      "keyi",
      "sda",
      "dasd"
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    current: 0,
    // color_list: [],
    // pic_list: [],
    // attrid_list: [],
    // nametemp_list:[],
    // headImg: '',
    goodsName: '',
    goodsDesc: '',
    goodsPrice: '',
    contentImg: '',
    goodsId: '',
    goodsNum: 1,
    // attrId: '',
    cartGoodsNum: 0,
    cartGoodsArrtIds: [],
    goodSelect:0//默认选择器
  },

  goindex() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },


  evaluate(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?id=' + e.currentTarget.dataset.goodsid,
    })
  },



  choiceColor(e){
    let that = this
    let temp = that.data.imgUrls
    temp.splice(0, 1, that.data.goodsAttribte[e.currentTarget.dataset.index].attr_img_url)
    that.setData({
      goodSelect: e.currentTarget.dataset.index,
      imgUrls: temp,
      current: 0,
      // attrId: that.data.attrid_list[e.currentTarget.dataset.index],
      // choiceColor: that.data.nametemp_list[e.currentTarget.dataset.index]
    })
  },

  fetchGoodsDetail(obj) {
    ajax
      .wxRequest({
        url: '/index.php/api/SApi/index/m/getPageGoodsInfo/',
        data: typeof obj === 'object' ? obj : {}
      })
      .then(res => {
        console.log(res)
        let data = res.datas
        let goods = res.datas.goods
        let goodsAttribte = goods.goods_attribute
        this.setData({
          imgUrls: data.pic_swiper,
          goodsName: goods.goods_name,
          goodsDesc: goods.goods_remark[0],
          goodsPrice: data.goods_price || '',
          contentImg: goods.goods_content_pic,
          goodsAttribte,
          product:res.product
        })
      })
  },

  fetchCartList() {
    ajax
      .wxRequest({
        url: '/index.php/api/SApi/index/m/cartIndex',
        data: {
          key: app.urlData.key,
          wid: app.urlData.wid
        }
      })
      .then(res => {
        if (res.status == 1) {
          if (Array.isArray(res.data)) {
            this.setData({
              cartGoodsArrtIds: res.data.map(item => item.attr_id)
            })
          }
          this.setData({
            cartGoodsNum: res.data.length
          })
        }
      })
  },

  addShopCart() {
    let _this = this
    ajax
      .wxRequest({
        url: '/index.php/api/SApi/index/m/addPageCart',
        data: {
          goods_id: _this.data.goodsId,
          goods_num: _this.data.goodsNum,
          attr_id: _this.data.goodsAttribte[_this.data.goodSelect].attr_id
        }
      })
      .then(res => {
        if (res.status == 1) {
          wx.showToast({
            title: '加入成功',
            icon: 'success'
          })
          if (!this.data.cartGoodsArrtIds.includes(_this.data.goodsAttribte[_this.data.goodSelect].attr_id)) {
            let cartGoodsArrtIds = this.data.cartGoodsArrtIds
            cartGoodsArrtIds.push(_this.data.goodsAttribte[_this.data.goodSelect].attr_id)

            this.setData({
              cartGoodsNum: _this.data.cartGoodsNum + 1,
              cartGoodsArrtIds: cartGoodsArrtIds
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
  },

  buyNow(e){
    let _this = this

    if (wx.getStorageSync('customerid') == '' || wx.getStorageSync('customerid') == 0){
      wx.showModal({
        title: '提示',
        content: '您还没有注册，是否去注册',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/phoneBinding/phoneBinding',
            })
          }
        }
      })
    }else{
      ajax
        .wxRequest({
          url: '/index.php/api/SApi/index/m/addPageCart',
          data: {
            goods_id: _this.data.goodsId,
            goods_num: _this.data.goodsNum,
            attr_id: _this.data.goodsAttribte[_this.data.goodSelect].attr_id,
            one: 1
          }
        })
        .then(res => {
          console.log(res)
          wx.navigateTo({
            url: '/pages/settlement/settlement',
          })
        })
    }
 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      goodsId: options.goods_id,
     
    })
    this.fetchGoodsDetail({
      goods_id: options.goods_id
    })
    this.fetchCartList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  onShareAppMessage:function(){
    let that = this
    return {
      title: "PAPARELA  " + that.data.goodsName,
      path: '/pages/details/details?goods_id=' + that.data.goodsId
    }
  }
})